package main.com.votes.dao;

import main.com.votes.model.Vote;

import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.sql.SQLException;

public class VoteDao extends BaseDAO{

    //    新增投票记录
    public int addVote(int articleId, int optionId, HttpServletRequest httpServletRequest) {
        String sql = "insert into vote values(0, ?,?,?)";
        return super.update(sql, new Object[]{articleId, optionId,
                httpServletRequest.getSession().getAttribute("userId")
        });
    }

//    检验我有没有投过
    public Vote isIHaveVote(int articleId, HttpServletRequest httpServletRequest) {
        String sql = "select * from  vote where articleid = ? and userid = ?";
        return super.select(sql, new Object[]{articleId,
                httpServletRequest.getSession().getAttribute("userId")
        });
    }

    @Override
    public Vote rowMapper(ResultSet rs) throws SQLException {
        return new Vote(
                rs.getInt(1),
                rs.getInt(2),
                rs.getInt(3),
                rs.getInt(4)
        );
    }

}
